package com.threadpool;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

/**
 * @author xuejingjing
 * @className ThreadPoolTest
 * @description
 * 时间:109
 * 大小100000
 * @date 2022/11/12/012 20:48
 */
public class ThreadPoolTest {
    public static void main(String[] args) throws InterruptedException {
         long start = System.currentTimeMillis();
         final Random random = new Random();
         final List<Integer> list = new ArrayList<>();
         // 创建线程池
        ExecutorService executorService = Executors.newSingleThreadExecutor();
        for (int i = 0; i < 100000; i++) {
            // 线程池执行任务
            executorService.execute(new Runnable() {
                @Override
                public void run() {
                    list.add(random.nextInt());
                }
            });
        }
        // 停止接收新任务，原来的任务继续执行
        // 配合是用来关闭线程池
        executorService.shutdown();
        executorService.awaitTermination(1, TimeUnit.DAYS);
        System.out.println("时间:"+(System.currentTimeMillis()-start));
        System.out.println("大小"+list.size());
    }
}
